package com.hantek.lms.login.controller;

import com.dz.core.annotation.ParamValidation;
import com.dz.core.component.support.JwtBean;
import com.dz.core.component.cache.RedisCache;
import com.dz.core.config.properties.JwtProperties;
import com.dz.core.constant.BizCode;
import com.dz.core.dto.BusResult;
import com.dz.core.dto.ReqBody;
import com.dz.core.dto.ResBody;
import com.google.common.base.Strings;
import com.hantek.lms.login.dto.UserDto;
import com.hantek.lms.login.model.DomainUser;
import com.hantek.lms.login.service.IDomainUserService;
import com.hantek.lms.login.vo.LoginUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class LoginController {

    @Autowired
    private JwtBean jwtBean;

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IDomainUserService iDomainUserService;


    @PostMapping("/login")
    @ParamValidation(bean = UserDto.class)
    public ResBody login(@RequestBody ReqBody<UserDto> reqBody) throws Exception{
        UserDto userDto = reqBody.getBody();

        DomainUser user = iDomainUserService.findUser(userDto);
        if(null == user || Strings.isNullOrEmpty(user.getSalt())){
            return ResBody.generate(BusResult.build(BizCode.FAIL));
        }




//        String pwdStr = userDto.getPwd() + userDto.getName() + user.getSalt();
//        String md5Pwd = MD5Util.MD5Encode(pwdStr,"utf-8");
//        if(!md5Pwd.endsWith(user.getPassword())){
//            return ResBody.generate(BusResult.buildFail("账号密码错误"));
//        }

        LoginUserVo userVo = new LoginUserVo();
        userVo.setUserName(user.getUsername());
        userVo.setRealName(user.getEmail());
//        BeanCopy.create(DomainUser.class,LoginUserVo.class).copy(user,userVo);

        String token = jwtBean.generateToken(userDto.getName(),jwtBean.getRandomKey());
        userVo.setToken(token);
        String refreshToken = jwtBean.generateRefreshToken(userDto.getName(),jwtBean.getRandomKey());
        userVo.setRefreshToken(refreshToken);

        redisCache.put(token,user,jwtProperties.getExpiration());

        DomainUser domainUser = redisCache.get(token,DomainUser.class);

        System.out.println("domainUser.toString() = " + domainUser.toString());

        return ResBody.generate(BusResult.build(BizCode.SUCCESS,userVo));
    }


    @GetMapping("/token")
    public ResBody refreshToken(@RequestParam String oldToken, HttpServletRequest request) throws Exception{

        final String token = request.getHeader(jwtProperties.getHeader());
        String username = jwtBean.getUsernameFromToken(token);
        String refreshToken = jwtBean.generateRefreshToken(username,jwtBean.getRandomKey());

        DomainUser user = redisCache.get(oldToken,DomainUser.class);
        redisCache.del(oldToken);
        redisCache.put(refreshToken,user,jwtProperties.getExpiration());

        return ResBody.generate(BusResult.build(BizCode.SUCCESS,refreshToken));
    }
}
